package com.dianban.controller.nengliang;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.FacePayStoreConsumeLog;
import com.dianban.entity.JwStore;
import com.dianban.entity.User;
import com.dianban.service.FacePayStoreConsumeLogService;
import com.dianban.service.JwStoreService;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value = "ApiConsumeLogController", description = "已核销使用记录控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/consumeLog/")
public class ApiConsumeLogController extends BaseController {
    @Autowired
    private FacePayStoreConsumeLogService facePayStoreConsumeLogService;
    @Autowired
    private JwStoreService storeService;

    // 获取我的使用记录
    @ApiOperation(notes = "list", httpMethod = "GET", value = "我的已核销使用列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
    })
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo list(HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        List<FacePayStoreConsumeLog> facePayStoreConsumeLogList = facePayStoreConsumeLogService.findByParams(null, null, user.getId(), null);
        if (!CollectionUtils.isEmpty(facePayStoreConsumeLogList)) {
            // 门店名称查询出来
            List<JwStore> storeList = storeService.findList("");
            Map<Long, String> storeMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(storeList)) {
                for (JwStore jwStore : storeList) {
                    storeMap.put(jwStore.getId(), jwStore.getName());
                }
            }
            for (FacePayStoreConsumeLog consumeLog : facePayStoreConsumeLogList) {
                consumeLog.setStoreName(storeMap.get(consumeLog.getStoreId()));
            }
        }
        return ResultVo.success(facePayStoreConsumeLogList, "操作成功");
    }


}
